﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetServer
{
    public class MyLogger
    {
        /// <summary>
        /// 卡片起始块号 
        /// </summary>
        public static int CARD_BLOCK = 3;
        /// <summary>
        /// 卡片密钥
        /// </summary>
        public static string CARD_KEY = "B6A8A3DC2BB9";
        /// <summary>
        /// 卡片数据DES密钥
        /// </summary>
        public static string CARD_DES = "B1A8C6A9B4D3A3D0";
        public static Dictionary<String, String> codeMap = new Dictionary<String, String>();
        /// <summary>
        /// 功能号
        /// </summary>
        public static void doInit()
        {
            try
            {
                codeMap.Add("01", "无效卡");
                codeMap.Add("02", "余额不足");

                codeMap.Add("03", "无效时段");

                codeMap.Add("04", "包餐超限");
                codeMap.Add("05", "包餐无效");

                codeMap.Add("06", "单次超限");
                codeMap.Add("07", "单日超限");

                codeMap.Add("08", "次数超限");
                codeMap.Add("09", "次数无效");

                codeMap.Add("10", "帐户无效");
                codeMap.Add("11", "数据库错误");

                codeMap.Add("12", "机器未认证");

                codeMap.Add("13", "卡已挂失");

                codeMap.Add("14", "消费区域限制");
            }
            catch (Exception e)
            {
            }
        }
        /// <summary>
        /// 指定长度字符串 不足补空格
        /// </summary>
        /// <param name="src">字符串</param>
        /// <param name="len">长度</param>
        /// <returns></returns>
        public static String formatTxt(String src, int len)
        {
            return formatTxt(src, len, " ", true);
        }
        /// <summary>
        /// 指定长度字符串 不足后面补充字符串
        /// </summary>
        /// <param name="src">字符串</param>
        /// <param name="len">长度</param>
        /// <param name="addC">补充字符串</param>
        /// <returns></returns>
        public static String formatTxt(String src, int len, String addC)
        {
            return formatTxt(src, len, addC, true);
        }
        /// <summary>
        /// 获取定长随机数
        /// </summary>
        /// <param name="len"></param>
        /// <returns></returns>
        public static String getRandom(int len)
        {
            int max = (int)Math.Pow(10.0D, len + 1) - 1;
            Random random = new Random();
            return formatInt(random.Next(max), len);
        }
        /// <summary>
        /// 指定长度 int 不足前面补充0
        /// </summary>
        /// <param name="num"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static String formatInt(int num, int len)
        {
            String str = num.ToString();
            return formatTxt(str, len, "0", false);
        }
        /// <summary>
        /// 获取定长的字符串
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="len">长度</param>
        /// <returns></returns>
        public static String getSubString(String str, int len)
        {
            byte[] bytes = ByteUtil.getBytes(str);
            if (bytes.Length > len)
            {
                byte[] copy = new byte[len];
                Array.Copy(bytes, copy, len);
                return getString(copy);
            }
            return str;
        }
        /// <summary>
        /// 指定长度字符串 不足补充字符串
        /// </summary>
        /// <param name="src">字符串</param>
        /// <param name="len">长度</param>
        /// <param name="addC">补充字符串</param>
        /// <param name="isAppend">true 后面补充 </param>
        /// <returns></returns>
        public static String formatTxt(String src, int len, String addC, bool isAppend)
        {
            if (src == null)
                src = "";
            String str = src;
            int dif_len = len - ByteUtil.getLength(str);
            for (int i = 0; i < dif_len; ++i)
            {
                if (isAppend)
                    str = str + addC;
                else
                {
                    str = addC + str;
                }
            }
            if (Encoding.Default.GetBytes(str).Length > len)
                str = getSubString(str, len);
            return str;
        }
        /// <summary>
        /// byte[]转换成String
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static String getString(byte[] bytes)
        {
            return System.Text.Encoding.Default.GetString(bytes);
        }
    }
}
